package com.cskaoyan.controller;

import com.cskaoyan.bean.CskaoyanmallAd;
import com.cskaoyan.service.CskaoyanmallAdService;
import com.cskaoyan.service.CskaoyanmallAdServiceImpl;
import com.cskaoyan.vo.AdVo;
import com.cskaoyan.vo.BaseRespVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class AddController {


    @Autowired
    CskaoyanmallAdService cskaoyanmallAdService = new CskaoyanmallAdServiceImpl();

    //http://localhost:8083/admin/ad/list?page=1&limit=20&sort=add_time&order=desc
    //http://182.92.235.201:8083/admin/ad/list?page=1&limit=20&name=qwe&content=qwe&sort=add_time&order=desc
    @RequestMapping("admin/ad/list")
    public BaseRespVo list(Integer page, Integer limit, String name, String content, String sort, String order) {
        AdVo adVo = cskaoyanmallAdService.query(page, limit,name,content,sort, order);
        return BaseRespVo.ok(adVo);
    }

    @RequestMapping("admin/ad/create")
    public BaseRespVo create(@RequestBody CskaoyanmallAd cskaoyanmallAd) {
        if (cskaoyanmallAd.getUrl() == null){
            return BaseRespVo.erro(400,"文件不能为空");
        }
        cskaoyanmallAd.setDeleted(false);
        int insert = cskaoyanmallAdService.insert(cskaoyanmallAd);
        if (insert == 1){
            return BaseRespVo.ok(cskaoyanmallAd,"创建成功");
        }
        return BaseRespVo.erro(400,"创建失败");
    }


    //http://localhost:8083/admin/ad/delete
    @RequestMapping("admin/ad/delete")
    public BaseRespVo delete(@RequestBody CskaoyanmallAd cskaoyanmallAd) {
        int delete = cskaoyanmallAdService.delete(cskaoyanmallAd);
        if ( delete ==0 ){
            return BaseRespVo.ok();
        }else{
            return BaseRespVo.erro(662,"删除失败");
        }

    }

    //http://182.92.235.201:8083/admin/ad/update
    @RequestMapping("admin/ad/update")
    public BaseRespVo update(@RequestBody CskaoyanmallAd cskaoyanmallAd) {
        int i = cskaoyanmallAdService.updateByPrimaryKey(cskaoyanmallAd);
        if ( i ==1 ){
            return BaseRespVo.ok("更新广告成功");
        }else{
            return BaseRespVo.erro(662,"插入失败");
        }

    }

}
